import axios from "axios";
import axiosRetry from "axios-retry";
import {XMLParser} from "fast-xml-parser";
//import { HttpsProxyAgent } from 'https-proxy-agent';

// const http = require("http")
// const https = require("http")
//var agent = new HttpsProxyAgent('http://127.0.0.1:2334');
//import SocksProxyAgent from 'socks-proxy-agent';

//const { createProxyMiddleware } = require('http-proxy-middleware');
const TIMEOUT = 20000

//const URL = require('url')
//const request = require('request')
// pass the axios instance to the retry function and call it.
axiosRetry(axios,{
    retries:3, // number of retries (defaults to 3)
});
// axios.defaults.proxy = {
//     host:'http://127.0.0.1',
//     port:6450
// }
const zy = {
    xmlConfig: {
        trimValues:true,
        textNodeName:'_t',
        ignoreAttributes: false,
        attributeNamePrefix: '_',
        parseAttributeValue: true
    },
    async getSite (key) {
        let api = await window.go.main.App.DbGet(key)
        return api
    },
    list (key,pg =1 ,t) {
        return new Promise( async (resolve,reject)=>{
            //const api = await this.getSite(key)
            const api = "https://91md.me/api.php/provide/vod/at/xml/"
            let url = null
            if (t){
                url = `${api}?ac=videolist&t=${t}&pg=${pg}`
            }else{
                url = `${api}?ac=videolist&pg=${pg}`
            }
            axios.get(url,{
                headers:{'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"},
                }).then(async res=>{
                const parser = new XMLParser();

                const data = res.data
                const json = parser.parse(data,this.xmlConfig)
                const jsondata = json.rss === undefined ? json:json.rss
                const videoList = jsondata.list.video
                if(videoList && videoList.length){
                    resolve(videoList)
                }else{
                    resolve([])
                }
            }).catch(err=>{reject(err)})
        })
    },
}

export default zy;